﻿/*************************************************************************************
 * 工    具：  HK_Dropdown
 * 
 * 描    述：  重写的UGUI Dropdown组件，通过在Inspector面板中修改AudioType播放对应的Button音效
 * 
 * 版    本：  V1.0
 * 作    者：  京产肠饭、郭加和
 * 
 * 创    建：  2022/12/07  V1.0
 * 
 * 链    接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System;
using UnityEngine.EventSystems;

namespace UnityEngine.UI
{
    [RequireComponent(typeof(Image))]
    [AddComponentMenu("UI/HK_Dropdown", 30)]
    public class HK_Dropdown : Dropdown
    {
        public enum AudioPressType
        {
            PressDefault,
            PressType1,
            PressType2,
            PressType3,
            PressType4
        }
        [SerializeField]
        AudioPressType audioPressType = AudioPressType.PressDefault;
        #region 事件类型
        public static event Action pressDefaultAudio_Event;
        public static event Action pressType1Audio_Event;
        public static event Action pressType2Audio_Event;
        public static event Action pressType3Audio_Event;
        public static event Action pressType4Audio_Event;
        #endregion

        public enum AudioSelectType
        {
            SelectDefault,
            SelectType1,
            SelectType2,
            SelectType3,
            SelectType4
        }
        [SerializeField] AudioSelectType audioSelectType = AudioSelectType.SelectDefault;

        #region 事件
        public static event Action selectDefaultAudio_Event;
        public static event Action selectType1Audio_Event;
        public static event Action selectType2Audio_Event;
        public static event Action selectType3Audio_Event;
        public static event Action selectType4Audio_Event;

        #endregion

        protected override void Awake()
        {
            base.Awake();

            // 改变选项的时候播放对应音效
            onValueChanged.AddListener(index =>
            {
                AudioSelect(audioSelectType);
            });
        }

        //重写系统自带的点击方法：添加点击音效方法
        public override void OnPointerClick(PointerEventData eventData)
        {
            base.OnPointerClick(eventData);
            AudioPress(audioPressType);
        }

        //点击播放音效
        void AudioPress(AudioPressType audioPressType)
        {
            UISystemProfilerApi.AddMarker("Button.onClick", this);

            switch (audioPressType)
            {
                case AudioPressType.PressDefault:
                    if (pressDefaultAudio_Event != null)
                        pressDefaultAudio_Event();
                    break;
                case AudioPressType.PressType1:
                    if (pressType1Audio_Event != null)
                        pressType1Audio_Event();
                    break;
                case AudioPressType.PressType2:
                    if (pressType2Audio_Event != null)
                        pressType2Audio_Event();
                    break;
                case AudioPressType.PressType3:
                    if (pressType3Audio_Event != null)
                        pressType3Audio_Event();
                    break;
                case AudioPressType.PressType4:
                    if (pressType4Audio_Event != null)
                        pressType4Audio_Event();
                    break;
            }
        }
        //选择音效
        void AudioSelect(AudioSelectType audioSelectType)
        {
            switch (audioSelectType)
            {
                case AudioSelectType.SelectDefault:
                    if (selectDefaultAudio_Event != null)
                        selectDefaultAudio_Event();
                    break;
                case AudioSelectType.SelectType1:
                    if (selectType1Audio_Event != null)
                        selectType1Audio_Event();
                    break;
                case AudioSelectType.SelectType2:
                    if (selectType2Audio_Event != null)
                        selectType2Audio_Event();
                    break;
                case AudioSelectType.SelectType3:
                    if (selectType3Audio_Event != null)
                        selectType3Audio_Event();
                    break;
                case AudioSelectType.SelectType4:
                    if (selectType4Audio_Event != null)
                        selectType4Audio_Event();
                    break;
            }
        }
    }
}
